<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>化学题解助手</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f9;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            padding: 0;
        }

        .container {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 80%;
            max-width: 600px;
        }

        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 20px;
        }

        textarea {
            width: 100%;
            height: 100px;
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            resize: vertical;
            font-size: 16px;
        }

        button {
            width: 100%;
            padding: 10px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #0056b3;
        }

        .result-container {
            margin-top: 20px;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            background-color: #f9f9f9;
            min-height: 100px;
            max-height: 300px; /* 设置最大高度以启用滚动条 */
            overflow-y: auto;
            font-size: 16px;
            color: #333;
        }

        .result-container pre {
            margin: 0;
            padding: 0;
            white-space: pre-wrap;
        }

        .loading {
            text-align: center;
            color: #555;
            font-size: 14px;
        }

        .error {
            text-align: center;
            color: #ff4d4d;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>化学题解助手</h1>
        <textarea id="userInput" placeholder="请输入你的化学问题..."></textarea>
        <button id="submitBtn">获取题解</button>
        <div id="resultContainer" class="result-container">
            <div id="loading" class="loading" style="display: none;">加载中...</div>
            <div id="error" class="error" style="display: none;"></div>
            <pre id="result"></pre>
        </div>
    </div>

    <script>
        document.getElementById('submitBtn').addEventListener('click', async () => {
            const userInput = document.getElementById('userInput').value;
            const resultContainer = document.getElementById('resultContainer');
            const resultDiv = document.getElementById('result');
            const loadingDiv = document.getElementById('loading');
            const errorDiv = document.getElementById('error');

            // Clear previous results and errors
            resultDiv.innerHTML = '';
            loadingDiv.style.display = 'block';
            errorDiv.style.display = 'none';

            try {
                const response = await fetch('https://ai.gitee.com/v1/chat/completions', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'Authorization': 'Bearer URUNAGMMW6JQ1HR4CKBNTZZHP1CC6I21ROIQLI1V',
                        'X-Failover-Enabled': 'true'
                    },
                    body: JSON.stringify({
                        model: "QwQ-32B",
                        stream: false,
                        max_tokens: 5000,
                        temperature: 1,
                        top_p: 0.2,
                        top_k: 50,
                        frequency_penalty: 0,
                        messages: [
                            {
                                "role": "system",
                                "content": "你是一位资深的化学教师，精通初高中化学知识，并且对中高考的所有化学题型了如指掌。请帮助学生解答化学问题。首先，明确学生的需求，然后提供详细的题解，逻辑清晰且易于理解。先给出题解，再详细分析解题过程并提供最终答案。化学式请用字符串表示。"
                            },
                            {
                                "role": "user",
                                "content": userInput
                            }
                        ],
                    })
                });

                if (!response.ok) {
                    throw new Error(`HTTP error! status: ${response.status}`);
                }

                const data = await response.json();
                const answer = data.choices[0].message.content;
                resultDiv.innerHTML = answer;
            } catch (error) {
                console.error('Error fetching the response:', error);
                errorDiv.textContent = '获取题解时出错，请稍后再试。';
                errorDiv.style.display = 'block';
            } finally {
                loadingDiv.style.display = 'none';
            }
        });
    </script>
</body>
</html>
